package com.ruoyi.demowechatpay.controller;

import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.bean.transfer.TransferBatchesResult;
import com.ruoyi.demowechatpay.service.WxPayServiceV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/api/pay")
public class WxPayV3Controller {

    @Autowired
    private WxPayServiceV3 wxPayServiceV3;

    /**
     * 发起JSAPI支付
     */
    @PostMapping("/jsapi")
    public WxPayUnifiedOrderV3Result jsapiPay(@RequestParam String openid,
                                              @RequestParam String outTradeNo,
                                              @RequestParam BigDecimal amount,
                                              @RequestParam String description) throws Exception {
        return wxPayServiceV3.jsapiPay(openid, outTradeNo, amount, description);
    }

    /**
     * 申请退款
     */
    @PostMapping("/refund")
    public WxPayRefundV3Result refund(@RequestParam String outTradeNo,
                                      @RequestParam String outRefundNo,
                                      @RequestParam BigDecimal totalAmount,
                                      @RequestParam BigDecimal refundAmount,
                                      @RequestParam String reason) throws Exception {

        //TODO: 根据实际业务去做处理
        return wxPayServiceV3.refund(outTradeNo, outRefundNo, totalAmount, refundAmount, reason);
    }

    /**
     * 提现到零钱（企业付款）
     */
    @PostMapping("/withdraw")
    public TransferBatchesResult withdraw(@RequestParam String openid,
                                          @RequestParam BigDecimal amount,
                                          @RequestParam String description) throws Exception {
        return wxPayServiceV3.transferToBalance(openid, amount, description);
    }

    /**
     * 查询订单
     */
    @GetMapping("/query")
    public WxPayOrderQueryV3Result queryOrder(@RequestParam String outTradeNo) throws Exception {
        return wxPayServiceV3.queryOrder(outTradeNo);
    }
}